package com.imooc.reader.interceptor;

import com.imooc.reader.enity.Member;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

// 拦截后台管理系统页面，必须进行登陆
public class LoginInterceptor implements HandlerInterceptor {

    //方法执行之前拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Member member = (Member) session.getAttribute("loginManagementMember");
        //true 放行 false拦截
        if(member!=null){
            return true;
        }
        //如果没有登录成功，或者没有先进入拦截页面进行登录，将跳转到拦截页面
        response.sendRedirect("/management/login.html");
        return false;
    }

    //方法接收请求之后，解析视图之前拦截
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    //方法执行完毕，视图渲染结束之后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
